#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(){

	int fd;
	char *buf = "zzxl 666!";

	fd = open ("./file1",O_RDWR);
	
	if(fd == -1){
		
		printf("open file1 failed!\n");
	}
	
	fd=open("./file1",O_RDWR|O_CREAT,0600);
	
	if(fd > 0){
		printf("creat file1 success\n");
	}
	printf("open success : fd=%d\n",fd);
	
//	ssize_t write(int fd, const void *buf, size_t count);
	int n_write = write(fd,buf,strlen(buf));
	if(n_write != -1){
		printf("write %d byte to file\n",n_write);
	}

//	off_t lseek(int fd, off_t offset, int whence);
	lseek(fd,0,SEEK_SET);


//	ssize_t read(int fd, void *buf, size_t count);
	char *readBuf = (char*)malloc(sizeof(char) * n_write + 1);
	int n_read = read(fd,readBuf,n_write);
	printf("read: %d,context: %s\n",n_read,readBuf);

	close(fd);

	return 0;
}
